package cn.exrick.xboot.modules.iocr.service.serviceimpl;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.modules.file.service.FileService;
import cn.exrick.xboot.modules.iocr.entity.ZJGLServiceIocr;
import cn.exrick.xboot.modules.iocr.mapper.ZJGLServiceIocrMapper;
import cn.exrick.xboot.modules.iocr.service.ZJGLServiceIocrService;
import cn.exrick.xboot.modules.iocr.util.IocrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 识别入库实体类接口实现
 * @author 王延超
 */
@Slf4j
@Service
@Transactional
public class ZJGLServiceIocrServiceImpl extends ServiceImpl<cn.exrick.xboot.modules.iocr.mapper.ZJGLServiceIocrMapper, ZJGLServiceIocr> implements ZJGLServiceIocrService {

    @Autowired
    private ZJGLServiceIocrMapper ZJGLServiceIocrMapper;
    @Autowired
    private FileService fileService;
    //电信模板
    public static String templateSignDX = "d9d522927ac9a858e58078b3b6bf2b92";
    //招商模板
    public static String templateSignZS = "67fadf76990bcd19eb003d9aa1552e15";
    //工商模板
    public static String templateSignGS = "e3211b1ae2c30ea872cb51ef1537569f";

    //模板确认
    public static String templateSign = "";

    @Override
    public int iocrServe(Map fileData) {
        List<Map> fileList = (List<Map>) fileData.get("data");
        Integer stu = (Integer) fileData.get("stu");
        if (stu==1){
            templateSign = templateSignDX;
        }else if (stu==2){
            templateSign = templateSignGS;
        }else if (stu==3){
            templateSign = templateSignZS;
        }else if (stu==4){
            templateSign = templateSignDX;
        }else{
            return 0;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        for (Map a: fileList) {
            Map response = (Map) a.get("response");
            String url = (String) response.get("result");
            String[] split = url.split("/");
            String id = split[split.length - 1];
            List<Map> list = null;
            try {
                list = IocrUtil.test(fileService.get(id).getUrl(),templateSign);
                System.out.println("执行");
            } catch (IOException e) {
                throw new RuntimeException("运行时发生异常" + e);
            }
            if (list!=null){
                ZJGLServiceIocr zjglServiceIocr = new ZJGLServiceIocr();
                zjglServiceIocr.setImgFileId(id);
                zjglServiceIocr.setImgYuLan(url);
                for (Map map : list) {
                    String word_name = (String) map.get("word_name");
                    String word = (String)map.get("word");
                    if (word_name.equals("流水号")){
                        zjglServiceIocr.setSerialNumber(word);
                    }else if (word_name.equals("交易时间")){
                        try {
                            zjglServiceIocr.setCollectionTime(formatter.parse(word));
                        } catch (ParseException e) {
                            throw new RuntimeException("日期格式错误" + e);
                        }
                    }else if (word_name.equals("金额(小写)")){
                        if (stu==1){
                            word = word.substring(0, word.length() - 2) + "." + word.substring(word.length() - 2);
                        }
                        zjglServiceIocr.setSum(word);
                    }else if (word_name.equals("付款单位")){
                        zjglServiceIocr.setPayer(word);
                    }else if (word_name.equals("备注")){
                        zjglServiceIocr.setRemark(word);
                    }else if (word_name.equals("凭证号")||word_name.equals("回单编号")){
                        zjglServiceIocr.setBillNumber(word);
                    }else if (word_name.equals("收款账号")){
                        zjglServiceIocr.setCreditCardNumbers(word);
                    }else if (word_name.equals("付款账号")){
                        zjglServiceIocr.setPaymentCardNumber(word);
                    }
                }
                ZJGLServiceIocrMapper.insert(zjglServiceIocr);
            }

        }
        return 1;
    }
}
